Attribute VB_Name = "Module1"
Public fMainForm As frmMain
Public g_iListenPort As Long
Public g_iTimeStamp As Long

Function InitSocket() As Boolean
    On Error GoTo ErrProc
    
    fMainForm.m_Socket.Bind g_iListenPort
    fMainForm.SetStatus "Log server is listening on port " & g_iListenPort, "Time stamp set to " & g_iTimeStamp & " sec"
    InitSocket = True
    Exit Function

ErrProc:
    MsgBox "Warning: Listen on " & g_iListenPort & " failed: " & vbCrLf & Err.Description
    InitSocket = False
    Exit Function
End Function


Sub Main()
    On Error Resume Next
    Dim strListenPort As String
    Dim strTimeStamp As String
    
    Set fMainForm = New frmMain
    Load fMainForm
    
    strListenPort = GetSetting(App.Title, "Settings", "UDPListenPort", 4002)
    strTimeStamp = GetSetting(App.Title, "Settings", "TimeStampInterval", 30)
    
    'port check
    If (CheckPort(strListenPort) = False) Then
        strListenPort = "4002"
        SaveSetting App.Title, "Settings", "UDPListenPort", strListenPort
    End If

    'time stamp interval check
    If (CheckTimeStamp(strTimeStamp) = False) Then
        strTimeStamp = "30"
        SaveSetting App.Title, "Settings", "TimeStampInterval", strTimeStamp
    End If
    
    g_iListenPort = strListenPort
    g_iTimeStamp = strTimeStamp
    
    Call InitSocket
    fMainForm.Show
End Sub

Function CheckPort(strPort As String) As Boolean
    'port check
    If (Not IsNumeric(strPort)) Then
        MsgBox "Port not number!"
        CheckPort = False
        Exit Function
    End If
    If strPort > 65535 Or strPort <= 0 Then
        MsgBox "Port invalid!"
        CheckPort = False
        Exit Function
    End If

    CheckPort = True
End Function

Function CheckTimeStamp(strTimeStamp As String) As Boolean
    'time stamp interval check
    If (Not IsNumeric(strTimeStamp)) Then
        MsgBox "Time stamp interval invalid!"
        CheckTimeStamp = False
        Exit Function
    End If
    If strTimeStamp < 0 Then
        MsgBox "Time stamp interval invalid!"
        CheckTimeStamp = False
        Exit Function
    End If

    CheckTimeStamp = True
End Function

